package com.jiyinit.web.core.activiti.service;

import com.jiyinit.web.core.activiti.entity.TabLeaveBillInfoEntity;
import com.jiyinit.web.core.activiti.entity.TabUserMemershipEntity;
import com.jiyinit.web.core.activiti.enums.ProcessFlowEnum;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ContextLoader;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p></p>
 * <p>总经理审批 监听
 * <PRE>
 * <BR>	修改记录
 * <BR>-----------------------------------------------
 * <BR>	修改日期			修改人			修改内容
 * </PRE>
 *
 * @author jack zh
 * @version 1.0
 * @date 2018/7/17
 * @since 1.0
 */

public class LvTaskUser12Listener implements TaskListener {


    //节点处理人
    private UserMemershipServiceImpl userMemershipService;


    private LeaveBillInfoServiceImpl leaveBillInfoService;


    private LeaveDealMindServiceImpl leaveDealMindService;

    @Override
    public void notify(DelegateTask delegateTask) {
        userMemershipService=(UserMemershipServiceImpl) ContextLoader.getCurrentWebApplicationContext().getBean("userMemershipServiceImpl");
        leaveBillInfoService=(LeaveBillInfoServiceImpl) ContextLoader.getCurrentWebApplicationContext().getBean("leaveBillInfoServiceImpl");
        leaveDealMindService=(LeaveDealMindServiceImpl) ContextLoader.getCurrentWebApplicationContext().getBean("leaveDealMindServiceImpl");

        //任务申请名字
        String taskKey= delegateTask.getTaskDefinitionKey();
        String exeId= delegateTask.getExecutionId();  //执行实例id
        TabUserMemershipEntity shipEntity=null;
        TabLeaveBillInfoEntity billEntity=new TabLeaveBillInfoEntity();

        billEntity.setExecutionId(exeId);
        List<TabLeaveBillInfoEntity> billList=leaveBillInfoService.findBySelective(billEntity);
        if(billList.size()>0) {  //查询下一个节点处理人
            billEntity=billList.get(0);
            shipEntity = new TabUserMemershipEntity();
//            shipEntity.setOrgId(billEntity.getOrgId());  //过滤掉部门判断
            shipEntity.setGroupKey(taskKey);
            shipEntity.setProcinstKey(ProcessFlowEnum.Leave.getCode());
            shipEntity.setIsDelete("0");  //未删除
            List<TabUserMemershipEntity> shipList= userMemershipService.findBySelective(shipEntity);

            String mainUserId="";
            //存在 设置处理人组
            for (TabUserMemershipEntity shipBean:shipList) {
                delegateTask.addCandidateUser(shipBean.getUserId()+"");
                if(shipBean.getIsMain().equals("1")) {mainUserId=shipBean.getUserId()+"";}
            }

            delegateTask.setAssignee(mainUserId);
            leaveDealMindService.defalutDealTask(delegateTask,mainUserId);
        }

    }
}
